<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>BOOST_DECLARE_IS_CALLABLE</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Convert 2.0">
<link rel="up" href="../supporting_tools.html" title="Supporting Tools">
<link rel="prev" href="boost_declare_has_member.html" title="BOOST_DECLARE_HAS_MEMBER">
<link rel="next" href="../supported_compilers.html" title="Supported Compilers">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="spirit-nav">
<a accesskey="p" href="boost_declare_has_member.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../supporting_tools.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../supported_compilers.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_convert.supporting_tools.boost_declare_is_callable"></a><a class="link" href="boost_declare_is_callable.html" title="BOOST_DECLARE_IS_CALLABLE">BOOST_DECLARE_IS_CALLABLE</a>
</h3></div></div></div>
<p>
        The BOOST_DECLARE_IS_CALLABLE macro extends the functionality provided by
        BOOST_DECLARE_HAS_MEMBER and allows to declare a trait which would then let
        introspect the existence of a named class member function <span class="emphasis"><em>callable
        with the supplied signature</em></span>.
      </p>
<p>
        For example, the following declarations introduce <code class="computeroutput"><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span></code>
        and <code class="computeroutput"><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span></code> traits:
      </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">local</span>
<span class="special">{</span>
    <span class="identifier">BOOST_DECLARE_IS_CALLABLE</span><span class="special">(</span><span class="identifier">can_call_funop</span><span class="special">,</span> <span class="keyword">operator</span><span class="special">());</span>
    <span class="identifier">BOOST_DECLARE_IS_CALLABLE</span><span class="special">(</span><span class="identifier">can_call_func</span><span class="special">,</span> <span class="identifier">func</span><span class="special">);</span>
<span class="special">}}</span>
</pre>
<p>
      </p>
<p>
        The traits allow to test if the supplied class has respectively <code class="computeroutput"><span class="keyword">operator</span><span class="special">()</span></code>
        and <code class="computeroutput"><span class="identifier">func</span><span class="special">()</span></code>
        member functions callable with the specified signature:
      </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">callable</span>
<span class="special">{</span>
    <span class="keyword">struct</span>  <span class="identifier">test1</span> <span class="special">{</span> <span class="keyword">int</span>  <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> <span class="special">}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test2</span> <span class="special">{</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)</span> <span class="special">{}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test3</span> <span class="special">{</span> <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">int</span><span class="special">)</span> <span class="special">{}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test4</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">int</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">();</span> <span class="special">}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test5</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="special">=</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">())</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">();</span> <span class="special">}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test6</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">T</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">();</span> <span class="special">}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test7</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">T</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">T</span><span class="special">)</span> <span class="keyword">const</span>  <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">T</span><span class="special">();</span> <span class="special">}};</span>

    <span class="keyword">struct</span>  <span class="identifier">test11</span> <span class="special">{</span> <span class="keyword">int</span>  <span class="identifier">func</span><span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> <span class="special">}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test12</span> <span class="special">{</span> <span class="keyword">void</span> <span class="identifier">func</span><span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)</span> <span class="special">{}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test13</span> <span class="special">{</span> <span class="keyword">void</span> <span class="identifier">func</span><span class="special">(</span><span class="keyword">int</span><span class="special">)</span> <span class="special">{}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test14</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">func</span><span class="special">(</span><span class="keyword">int</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">();</span> <span class="special">}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test15</span> <span class="special">{</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">func</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="special">=</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">())</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">();</span> <span class="special">}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test16</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">func</span><span class="special">(</span><span class="identifier">T</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">();</span> <span class="special">}};</span>
    <span class="keyword">struct</span>  <span class="identifier">test17</span> <span class="special">{</span> <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">T</span> <span class="identifier">func</span><span class="special">(</span><span class="identifier">T</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">T</span><span class="special">();</span> <span class="special">}};</span>
<span class="special">}}</span>
</pre>
<p>
      </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test1</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test1</span><span class="special">,</span> <span class="keyword">double</span> <span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test1</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test1</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test1</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test1</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">int</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test1</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">(</span><span class="keyword">double</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">));</span>

<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test2</span><span class="special">,</span> <span class="keyword">int</span>  <span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test2</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test2</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span>   <span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_funop</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test2</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span>   <span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
</pre>
<p>
      </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test11</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test11</span><span class="special">,</span> <span class="keyword">double</span> <span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test11</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test11</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test11</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test11</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">int</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test11</span><span class="special">,</span> <span class="keyword">int</span> <span class="special">(</span><span class="keyword">double</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">));</span>

<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test12</span><span class="special">,</span> <span class="keyword">int</span>  <span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test12</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span><span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test12</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span>   <span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
<span class="identifier">BOOST_TEST</span><span class="special">((</span><span class="identifier">local</span><span class="special">::</span><span class="identifier">can_call_func</span><span class="special">&lt;</span><span class="identifier">callable</span><span class="special">::</span><span class="identifier">test12</span><span class="special">,</span> <span class="keyword">void</span> <span class="special">(</span>   <span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*)&gt;::</span><span class="identifier">value</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">));</span>
</pre>
<p>
      </p>
<p>
        As it can be seen from the example the traits check for the existence of
        a <span class="emphasis"><em>callable</em></span> member function but <span class="emphasis"><em>not necessarily
        of the specified signature</em></span>. Please check the <span class="emphasis"><em>Boost.TTI</em></span>
        library for the latter.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2009-2022 Vladimir
      Batov<p>
        Distributed under the Boost Software License, Version 1.0. See copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_declare_has_member.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../supporting_tools.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../supported_compilers.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
